﻿using ActionMailer.Net.Mvc;
using SocialASP.Models;
using System;
using System.Web.Mvc;

namespace SocialASP.Controllers
{
    public static class ExtensionMethods
    {


        public static string ToPublicUrl(this UrlHelper urlHelper, Uri relativeUri)
        {
            var httpContext = urlHelper.RequestContext.HttpContext;

            var uriBuilder = new UriBuilder
            {
                Host = httpContext.Request.Url.Host,
                Path = "/",
                Port = 80,
                Scheme = "http",
            };

            if (httpContext.Request.IsLocal)
            {
                uriBuilder.Port = httpContext.Request.Url.Port;
            }

            return new Uri(uriBuilder.Uri, relativeUri).AbsoluteUri;
        }
    }
    public class MailerController : MailerBase
    {
        public virtual EmailResult Activate(string email_, string fullName_, string id_)
        {
            To.Add(email_);
            Subject = "Welcome to " + AppHelper.ApplicationName + "!";
            ViewBag.FullName = fullName_;
            ViewBag.Email = email_;
            var urlhelper = new UrlHelper(base.HttpContextBase.Request.RequestContext);

            ViewBag.BaseUrl = urlhelper.Action("Activated", "Account", new { Id = id_ });
            ViewBag.BaseUrl = urlhelper.ToPublicUrl(new Uri(ViewBag.BaseUrl, UriKind.Relative));
            ViewBag.Id = id_;
            return Email("Activate");
        }

        public virtual EmailResult Welcome(string email_, string fullName_)
        {
            To.Add(email_);
            Subject = AppHelper.ApplicationName + " Account Activated!";
            ViewBag.FullName = fullName_;
            ViewBag.Email = email_;
            return Email("Welcome");
        }

    }
}
